package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Category;
import com.example.entity.Tag;
import com.example.service.CategoryService;
import com.example.mapper.CategoryMapper;
import com.example.utils.ResponseResult;
import com.example.utils.SecurityUtils;
import com.example.utils.SystemConstants;
import com.example.vo.PageVo;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author sprite-pc
* @description 针对表【sg_category(分类表)】的数据库操作Service实现
* @createDate 2023-12-31 14:15:55
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService{

    @Override
    public List<Category> listAllCategory() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getDelFlag, SystemConstants.STATUS_NORMAL);
        return list(queryWrapper);
    }

    @Override
    public ResponseResult getList(int pageNum, int pageSize,String name,String status) {
        Page<Category> categoryPage = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        if (name!=null){
            queryWrapper.like(Category::getName,name);
        }
        if (status!=null){
            queryWrapper.eq(Category::getStatus,status);
        }
        queryWrapper.eq(Category::getCreateBy, SecurityUtils.getUserId());
        page(categoryPage,queryWrapper);
        PageVo pageVo = new PageVo(categoryPage.getRecords(),categoryPage.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult saveCategory(Category category) {
        category.setCreateBy(SecurityUtils.getUserId());
        category.setPid(-1L);
        category.setCreateTime(new Date());
        save(category);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteCategory(List<Long> ids) {
        removeByIds(ids);
        return ResponseResult.okResult();
    }
}




